package org.jbit.news.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import org.jbit.news.biz.NewsBiz;
import org.jbit.news.biz.impl.NewsBizImpl;
import org.jbit.news.entity.News;

import com.alibaba.fastjson.JSON;

public class DoNewsListServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		NewsBiz newsBiz=new NewsBizImpl();	
		List<News> list=newsBiz.getAllnews();	
		PrintWriter out =response.getWriter();
//	    News news=null;
	    //使用ajax返回HTML内容
//	    StringBuffer  newList= new StringBuffer("");
//	    for (int i = 0; i < list.size();) {
//	    	news=list.get(i);
//	    	newList.append("<li>"+ news.getNtitle()+ "<span > 作者："
//					+ news.getNauthor()+ "&nbsp;&nbsp;&nbsp;&nbsp;"
//					+ "<a href='#'>修改</a> &nbsp;&nbsp;&nbsp;&nbsp;"
//					+ "<a href='#' onclick='return clickdel()'>删除</a>"
//					+ "</span></li>");
//	if ((++i) % 5 == 0) {
//		newList.append("<li class='space'></li>");
//
//	}
//
//		}
//		
//		out.print(newList);
		
		//end ajax返回HTML内容
		/*HttpSession session=request.getSession(); 
		session.setAttribute("list",list);
		request.getRequestDispatcher("../newspages/admin.jsp").forward(request, response);*/
		//josn格式返回

	/*	StringBuffer jsonl=new StringBuffer("[");
		for (int i = 0; i < list.size(); i++) {
			news=list.get(i);
			jsonl.append("{\"nid\":\""+news.getNid()+"\","+"\"ntitle\":\""+news.getNtitle().replace("\"", "")+"\","+"\"nauthor\":\""+news.getNauthor().replace("\"", "")+"\"}");
			if (i<list.size()-1) {
				jsonl.append(",");
			}
		}
		jsonl.append("]");
		System.out.println(jsonl.toString());
		out.print(jsonl);
		out.flush();
     	out.close();*/
		//josn格式返回
     	//fastJSON处理
     	String newsJSON=JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd HH:mm:ss");
     	out.print(newsJSON);
     	out.flush();
     	out.close();
     	
     	
     	
     	//fastJSON处理
	}

}
